<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="800px">
    <ContentWrap>
      <!-- 已绑定地址 -->
      <el-table v-loading="loading" :data="bindedList" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="绑定地址全称" align="left" prop="bindAddrName" />
        <el-table-column
          label="绑定时间"
          align="center"
          prop="createTime"
          width="180"
          :formatter="dateFormatter('YYYY-mm-dd HH:MM:SS')"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="100"
        >
          <template #default="scope">
            <el-button link type="primary" @click="handleDelete(scope.row)">解除绑定</el-button>
          </template>
        </el-table-column>
      </el-table>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="unBindParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="地址" prop="keyWord">
          <el-input placeholder="请输入地址" v-model.trim="unBindParams.keyWord" />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="unbindList" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="房屋地址" align="left" prop="houseName" />
        <el-table-column label="房间" align="left" prop="roomName" v-if="unBindParams.positionType == 'room'" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="100"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleBind(scope.row)"
              v-if="(scope.row.parentId != 0 && unBindParams.positionType == 'room') || (scope.row.parentId == 0 && unBindParams.positionType == 'house')"
              >绑定</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="unBindParams.pageNo"
        v-model:limit="unBindParams.pageSize"
        @pagination="getUnbindList"
      />
    </ContentWrap>
  </Dialog>
</template>
<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import {
  createEquipBind,
  deleteEquipBind,
  getEquipBindList,
  listUnBindRooms,
  listUnBinHouses
} from '@/api/zwsj/equipBind'

defineOptions({ name: 'EquipBind' })
const emits = defineEmits(['refreshList'])

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(true) // 列表的加载中
const bindedList = ref() // 已绑定房间列表的数据

const unbindList = ref() // 未绑定房间列表的数据
const total = ref(0) // 列表的总页数

const bindedParams = reactive({
  equipId: undefined,
  equipType: undefined
})

const unBindParams = reactive({
  pageNo: 1,
  pageSize: 10,
  keyWord: undefined,
  commId: undefined,
  equipId: undefined,
  equipType: undefined,
  positionType: undefined
})

/** 打开弹窗 */
const equipIdRef = ref() // 搜索的表单
const equipTypeRef = ref() // 搜索的表单
const open = async (equipId: number, equipType: string,positionType: string, commId: number, equipName: string) => {
  dialogVisible.value = true
  if(positionType == 'room'){
    dialogTitle.value = equipName + '—已关联房间地址'
  }else if(positionType == 'house'){
    dialogTitle.value = equipName + '—已关联房屋地址'
  }
  unBindParams.equipId = equipId
  unBindParams.equipType = equipType
  unBindParams.positionType = positionType
  unBindParams.commId = commId
  getUnbindList()

  bindedParams.equipId = equipId
  bindedParams.equipType = equipType
  getBindedList()

  equipIdRef.value = equipId
  equipTypeRef.value = equipType
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const queryFormRef = ref() // 搜索的表单

/** 查询已绑定地址列表 */
const getBindedList = async () => {
  loading.value = true
  try {
    const data = await getEquipBindList(bindedParams)
    bindedList.value = data
  } finally {
    loading.value = false
  }
}

/** 查询未绑定地址列表 */
const getUnbindList = async () => {
  loading.value = true
  try {
    unBindParams.keyWord = unBindParams.keyWord || ''
    if(unBindParams.positionType == 'room'){
      const data = await listUnBindRooms(unBindParams)
      unbindList.value = data.list
      total.value = data.total
    }else if(unBindParams.positionType == 'house'){
      const data = await listUnBinHouses(unBindParams)
      unbindList.value = data.list
      total.value = data.total
    }
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  unBindParams.pageNo = 1
  getUnbindList()
}

const handleBind = async (row: any) => {
  await createEquipBind({
    equipType: equipTypeRef.value,
    equipId: equipIdRef.value,
    bindAddrId: row.id
  })

  getBindedList()
  getUnbindList()
  emits('refreshList')
}
/** 删除按钮操作 */
const handleDelete = async (row: any) => {
  const id = row.id
  ElMessageBox.confirm('是否确认删除设备绑定信息编号为"' + id + '"的数据项?', 'warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
    .then(() => {
      deleteEquipBind(id).then((data) => {
        if (data) {
          ElNotification({
            type: 'success',
            title: `成功`,
            message: '删除成功'
          })
          getBindedList()
          getUnbindList()
          emits('refreshList')
        }
      })
    })
    .catch((e) => {
      ElNotification({ type: 'info', message: e })
    })
}
</script>
